package com.googlecode.mad.mvntools.common.core;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import com.googlecode.mad.mvntools.common.interfaces.IMavenVersionUtil;

/**
 * this Util is parsing the pom.properties file and gets provides the buildnumber and builddate <br>
 * <br>
 * LastChangedBy: $Author: marthaler.worb@gmail.com $<br>
 * LastChangedDate: $Date: 2010-12-29 13:23:14 +0000 (Wed, 29 Dec 2010) $<br>
 * 
 * @author (daniel marthaler)
 * @version $Revision: 10 $
 * @since 24.12.2010
 */
@SuppressWarnings("rawtypes")
public final class MavenVersionUtil implements IMavenVersionUtil {
    
    private static final String       ENCODING      = "UTF-8";
    
    private static final String       VERSION       = "version=";
    
    private final String              sourcepattern = "#EEE MMM dd HH:mm:ss z yyyy";
    
    private final String              targetpattern = "dd.MM.yy HH:mm";
    
    private final String              file          = "META-INF/maven/[group]/[artid]/pom.properties";
    
    private final Map<String, String> versionMap    = new HashMap<String, String>();
    
    private final Map<String, String> dateMap       = new HashMap<String, String>();
    
    public MavenVersionUtil() {
        
    }
    
    /*
     * (non-Javadoc)
     * @see com.googlecode.mad.mvntools.common.core.IMavenVersionUtil#getDateString(java.lang.String, java.lang.String)
     */
    public String getDateString(final String groupid, final String artifactid, final Class root) {
        this.init(this.file.replace("[group]", groupid).replace("[artid]", artifactid), root);
        
        return this.dateMap.get(this.file.replace("[group]", groupid).replace("[artid]", artifactid));
    }
    
    /*
     * (non-Javadoc)
     * @see com.googlecode.mad.mvntools.common.core.IMavenVersionUtil#getVersionString(java.lang.String, java.lang.String)
     */
    public String getVersionString(final String groupid, final String artifactid, final Class root) {
        this.init(this.file.replace("[group]", groupid).replace("[artid]", artifactid), root);
        return this.versionMap.get(this.file.replace("[group]", groupid).replace("[artid]", artifactid));
    }
    
    /*
     * (non-Javadoc)
     * @see com.googlecode.mad.mvntools.common.core.IMavenVersionUtil#getWholeVersionString(java.lang.String, java.lang.String)
     */
    public String getWholeVersionString(final String groupid, final String artifactid, final Class root) {
        return this.getVersionString(groupid, artifactid, root) + "; " + this.getDateString(groupid, artifactid, root);
    }
    
    public String getMavenString(final String groupid, final String artifactid, final Class root) {
        
        final String first = "*******************************************************************************";
        
        final StringBuilder b = new StringBuilder();
        b.append("\n");
        b.append(first);
        b.append("\n");
        b.append("*** ");
        b.append(artifactid);
        b.append(": ");
        b.append(this.getVersionString(groupid, artifactid, root));
        b.append(", built on ");
        b.append(this.getDateString(groupid, artifactid, root));
        
        while ((b.length() + 4) < first.length()) {
            b.append(" ");
        }
        b.append("    ");
        b.append("\n");
        b.append(first);
        return b.toString();
    }
    
    private void init(final String file, final Class root) {
        
        final String date = this.dateMap.get(file);
        
        if ((date != null) && !date.equals("")) {
            return;
        }
        
        root.getClassLoader();
        final URL url = root.getClassLoader().getResource(file);
        
        if (url == null) {
            return;
        }
        
        InputStream oin = null;
        try {
            oin = new BufferedInputStream(url.openStream());
        } catch (final IOException e1) {
            
        }
        
        String line;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(oin, MavenVersionUtil.ENCODING));
        } catch (final UnsupportedEncodingException e) {
            
        }
        try {
            while ((line = reader.readLine()) != null) {
                
                if (line.contains("CEST") || line.contains("CET")) {
                    final SimpleDateFormat format = new SimpleDateFormat(this.sourcepattern, Locale.US);
                    try {
                        final SimpleDateFormat fm = new SimpleDateFormat(this.targetpattern);
                        
                        this.dateMap.put(file, fm.format(format.parse(line)));
                    } catch (final ParseException e) {
                        
                    }
                    
                }
                
                if (line.contains("version")) {
                    
                    this.versionMap.put(file, line.replace(MavenVersionUtil.VERSION, ""));
                }
            }
        } catch (final IOException e) {
            
        }
    }
}
